/*
* @Author: Administrator
* @Date:   2019-08-18 16:22:05
* @Last Modified by:   Administrator
* @Last Modified time: 2019-08-18 16:55:45
*/
var http = require('http');

var fs = require('fs');


var server = http.createServer(function (req, res) {
	
	var url = req.url;
	console.log( url );

	if( url === '/' ){
		fs.readFile('./template/index.html', 'utf8', function (error, dataString) {

			res.end(dataString);

		});
	}else if( url.indexOf('.html') > -1 ){

		fs.readFile('./template' + url, 'utf8', function (error, dataString) {

			res.end(dataString);

		});

	}else if(url.indexOf('css') > -1){

		fs.readFile('./template' + url, 'utf8', function (error, dataString) {
			res.end(dataString);
		});

	}else if(url.indexOf('images') > -1){

		// 注意：在读取图片的时候不要设置编码		
		fs.readFile( './template' + url, function(err, file) {
            if (err) {
                console.log(err);
                return;
            } else {
                res.end(file);
            }
        });



	}else if( url.indexOf('js') > -1){

		fs.readFile('./template' + url, 'utf8', function (error, dataString) {
			res.setHeader('Content-Type', 'application/javascript');
			res.end(dataString);
		});


	}else{

		fs.readFile('./template/notfound.html', function (error, dataString) {
			res.end(dataString);
		});

	}

});

server.listen(8080, function () {
	console.log( 'server running...' );
})
